package com.mandala.attributeMapper;

import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.ldap.core.AttributesMapper;

import com.mandala.entry.Role;

public class RoleAttributeMapper implements AttributesMapper{

	@Override
	public Object mapFromAttributes(Attributes attr) throws NamingException {
		Role roleObject = new Role();
		roleObject.setCn((String)attr.get("cn").get());
		roleObject.setCode((String)attr.get("code").get());
		roleObject.setResourceName((String)attr.get("resourcename").get());
		roleObject.setOrder((String)attr.get("order").get());
		roleObject.setStatus((String)attr.get("status").get());
		roleObject.setIsDelete((String)attr.get("isdelete").get());
		if (attr.get("systemid") != null) {
			roleObject.setSystemId((String)attr.get("systemid").get());
		}
		if (attr.get("memberurl") != null) {
			roleObject.setSystemId((String)attr.get("memberurl").get());
		}
		roleObject.setType((String)attr.get("type").get());
		roleObject.setDescription((String)attr.get("description").get());
		roleObject.setScope((String)attr.get("scope").get());
		if(attr.get("permission") != null){
			int a = attr.get("permission").size();
			List<String> listPermission = new ArrayList<>();
			for (int i = 0; i < a; i++) {
				listPermission.add(attr.get("permission").get(i).toString());
			}
			roleObject.setPermission(listPermission);
		}
		
		if(attr.get("group") != null){
			int b = attr.get("group").size();
			List<String> listGroup = new ArrayList<>();
			for (int i = 0; i < b; i++) {
				listGroup.add(attr.get("group").get(i).toString());
			}
			roleObject.setGroup(listGroup);
		}
		
		if(attr.get("role") != null){
			int c = attr.get("role").size();
			List<String> listOrg = new ArrayList<>();
			for (int i = 0; i < c; i++) {
				listOrg.add(attr.get("org").get(i).toString());
			}
			roleObject.setOrg(listOrg);
		}
		
		if(attr.get("user") != null){
			int d = attr.get("user").size();
			List<String> listUser = new ArrayList<>();
			for (int i = 0; i < d; i++) {
				listUser.add(attr.get("user").get(i).toString());
			}
			roleObject.setUser(listUser);
		}
		
		if(attr.get("subrole") != null){
			int e = attr.get("subrole").size();
			List<String> listSubRole = new ArrayList<>();
			for (int i = 0; i < e; i++) {
				listSubRole.add(attr.get("subrole").get(i).toString());
			}
			roleObject.setSubRole(listSubRole);
		}
		
		/*System.out.println(attr.get("user"));
		System.out.println(attr.get("group"));
		System.out.println(attr.get("permission"));
		System.out.println(attr.get("org"));
		System.out.println(attr.get("subrole"));*/

		return roleObject;
	}

}
